#include "epages.h"

EPages::EPages(int x,int y,int w,int h) : CGroup("",10,SP_ALIGN_NONE)
{
    position(x,y);
    size(w,h);
    box(FL_PLASTIC_DOWN_BOX);

    header=new CGroup("",5,SP_ALIGN_NONE);
    header->position(x+4,y+2);
    header->size(w-8,40);
        yeni=new CButton(SP_ADD_BUTTON,SP_ALIGN_NONE,"",THM_BUTTON_NORMAL);
        yeni->position(15,y+7);
        yeni->size(30,30);
        yeni->color(FL_SELECTION_COLOR);
        yeni->down_box(FL_PLASTIC_DOWN_BOX);
        yeni->down_color(FL_SELECTION_COLOR);
        yeni->box(FL_PLASTIC_UP_BOX);
        yeni->callback(yeni_CB,this);

        ac=new CButton(SP_OPEN_BUTTON,SP_ALIGN_NONE,"",THM_BUTTON_NORMAL);
        ac->color(FL_SELECTION_COLOR);
        ac->down_box(FL_PLASTIC_DOWN_BOX);
        ac->down_color(FL_SELECTION_COLOR);
        ac->position(50,y+7);
        ac->size(30,30);
        ac->box(FL_PLASTIC_UP_BOX);

        kaldir=new CButton(SP_DELETE_BUTTON,SP_ALIGN_NONE,"",THM_BUTTON_NORMAL);
        kaldir->position(85,y+7);
        kaldir->size(30,30);
        kaldir->color(FL_SELECTION_COLOR);
        kaldir->down_box(FL_PLASTIC_DOWN_BOX);
        kaldir->down_color(FL_SELECTION_COLOR);
        kaldir->box(FL_PLASTIC_UP_BOX);
        kaldir->deactivate();
        kaldir->callback(kaldir_CB,this);

        kaydet=new CButton(SP_SAVE_AS_BUTTON,SP_ALIGN_NONE,"",THM_BUTTON_NORMAL);
        kaydet->position(120,y+7);
        kaydet->size(30,30);
        kaydet->color(FL_SELECTION_COLOR);
        kaydet->down_box(FL_PLASTIC_DOWN_BOX);
        kaydet->down_color(FL_SELECTION_COLOR);
        kaydet->box(FL_PLASTIC_UP_BOX);
        kaydet->callback(code_CB,this);

    header->box(FL_PLASTIC_DOWN_BOX);
    header->color(FL_SELECTION_COLOR);
    header->end();

    list=new Fl_Hold_Browser(x+5,y+45,w-10,h-52);
    list->box(FL_PLASTIC_DOWN_BOX);
    list->color(FL_SELECTION_COLOR);
    list->textcolor(FL_SELECTION_COLOR);
    list->textfont(FL_BOLD);
    list->callback(liste_CB,this);
    list->scrollbar.box(FL_PLASTIC_DOWN_BOX);
    list->deactivate();
    sahip=(Ewindow *)parent();
    pageCount=0;
}

EPages::~EPages()
{
    //dtor
}

void EPages::yeni_CB(Fl_Widget *p, void *userdata)
{
    EPages *gecici=(EPages *)userdata;
    gecici->yeni_CB2(p);
}
void EPages::yeni_CB2(Fl_Widget *p)
{
    list->activate();
    EBoard *tahta=new EBoard(224,10,800,620);
    std::string sayi="Soru"+sirala(pageCount+1);
    list->add(sayi.c_str());
    sahip->ekleBoard(tahta);
    sahip->redraw();
    list->redraw();
    kaldir->activate();
    pageCount++;
    list->select(pageCount);
    Ewindow *s=(Ewindow *)parent();
    s->label(sirala(pageCount));
}

void EPages::liste_CB(Fl_Widget *p, void *userdata)
{
    EPages *gecici=(EPages *)userdata;
    gecici->liste_CB2(p);
}
void EPages::liste_CB2(Fl_Widget *p)
{
    sahip->aktifEt(list->value()-1);
    sahip->label(sirala(list->value()-1));
}

void EPages::kaldir_CB(Fl_Widget *p, void *userdata)
{
    EPages *gecici=(EPages *)userdata;
    gecici->kaldir_CB2(p);
}
void EPages::kaldir_CB2(Fl_Widget *p)
{
    if(pageCount>0)
    {
        sahip->kaldirBoard();
        list->remove(list->value());
        pageCount--;
        sahip->label(sirala(pageCount).c_str());
        list->select(pageCount);
        sahip->aktifEt(pageCount);
    }
    else
    {
        kaldir->deactivate();
        list->deactivate();
    }
}

std::string EPages::sirala(int sayi)
{
    std::string git;
    char dizi[10]={'0','1','2','3','4','5','6','7','8','9'};
    int kalan=0;
    int sonuc=sayi/10;
    if(sonuc<1)
    {
        git=dizi[sayi];
    }
    else
    {
        sonuc=sayi/100;
        if(sonuc<1)
        {
            sonuc=sayi/10;
            git=dizi[sonuc];
            kalan=sayi%10;
            git+=dizi[kalan];
        }
        else
        {
            sonuc=sayi/100;
            git=dizi[sonuc];
            kalan=sayi%100;
            if(kalan<10)
            {
                git+=dizi[0];
                git+=dizi[kalan];
            }
            else
            {
                sonuc=kalan/10;
                git+=dizi[sonuc];
                kalan=kalan%10;
                git+=dizi[kalan];
            }

        }
    }
    return git;
}

void EPages::code_CB(Fl_Widget *p,void *userdata)
{
    EPages *gecici=(EPages *)userdata;
    gecici->code_CB2(p);
}

void EPages::code_CB2(Fl_Widget *p)
{
    eTextWindow *txtwin=new eTextWindow(800,600);
    txtwin->show();
    txtwin->codeal()->value(sahip->yazdir().c_str());
}

int EPages::sorusayisi()
{
    return pageCount;
}
